package com.xiaoran.springcloud.alibaba.service.Impl;

import com.xiaoran.springcloud.alibaba.dao.OrderDao;
import com.xiaoran.springcloud.alibaba.domain.Order;
import com.xiaoran.springcloud.alibaba.service.AccountService;
import com.xiaoran.springcloud.alibaba.service.OrderServce;
import com.xiaoran.springcloud.alibaba.service.StorageService;
import io.seata.spring.annotation.GlobalLock;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class OrderServiceIMpl implements OrderServce {
    @Resource
    private OrderDao orderDao;
    @Resource
    private StorageService storageService;
    @Resource
    private AccountService accountService;

    @GlobalTransactional(name = "fsp-create-order",rollbackFor = Exception.class)
    public void create(Order order) {
        log.info("--------->开始新建订单");
         orderDao.create(order);
         log.info("--------------->订单微服务开始调用库存，做扣减Count");
         storageService.decrease(order.getProduct_id(),order.getCount());

        log.info("--------------->订单微服务开始调用账户，做扣钱");
        accountService.delete(order.getUser_id(),order.getMoney());

        log.info("--------->修改的订单状态从0到1");
        orderDao.update(order.getUser_id(),0);

    }


}
